<%@ include file="/common/taglibs.jsp"%>
<%@ page import="idv.takeshi.software.productline.bookstore.domain.model.order.Order"%>
<%@ page import="idv.takeshi.software.productline.bookstore.domain.model.order.CreditCard"%>
<c:set var="goToFillUpOrderInfoUrl"><spring:url value="/order/orderDetailsInfo.html"/></c:set>
<c:set var="goToPersistOrderUrl"><spring:url value="/order/persistOrder.html"/></c:set>

<c:set var="goToPayByCreditCardUrl"><spring:url value="/payment/payByCreditCard.html">
	<spring:param name="orderId" value="${order.id}"/>
	<spring:param name="previousPath" value="${param.previousPath}"/>
	<spring:param name="seeOrderDetailsInfo" value="${param.seeOrderDetailsInfo}"/>
</spring:url></c:set>

<c:set var="goToPreviousPathUrl"><spring:url value="${param.previousPath}"/></c:set>

<head>
	<title><spring:message code="confirmOrder.header"/></title>
	<script type="text/javascript">
	$(document).ready(function(){
		$("#goToFillUpOrderInfo").click(function(){
			location.assign("${goToFillUpOrderInfoUrl}");
		});

		$("#confirm").click(function(){
			location.assign("${goToPersistOrderUrl}");
		});

		$("#goBackToPreviousPath").click(function(){
			location.assign("${goToPreviousPathUrl}");
		});

		$("#payByCreditCard").click(function(){
			location.assign("${goToPayByCreditCardUrl}");
		});
		
	});
	</script>
</head>
<div class="ui-widget summary-booklist">
	<h1 class="ui-helper-reset ui-widget-header ui-corner-top booklist-header">
		<spring:message code="orderDetailsInfo.header"/>
	</h1>
	<div class="ui-widget-content ui-corner-bottom">
	<table width="100%">
		<tr>
			<td>
			<h1><spring:message code="confirmOrder.bookList.header"/></h1>
			<c:if test="${param.seeOrderDetailsInfo}">
				<h3><spring:message code="confirmOrder.order.id"/>: <c:out value="${order.id}"/></h3>
			</c:if>
			
			<table width="100%" class="shoppingCart">
				<tr>
					<th><spring:message code="shoppingCart.item.book.name"/></th>
					<th><spring:message code="shoppingCart.item.book.unitPrice"/></th>
					<th><spring:message code="shoppingCart.item.quantity"/></th>
					<th><spring:message code="shoppingCart.item.total"/></th>
					<th><spring:message code="shoppingCart.item.book.inventory.status"/></th>
					<th><spring:message code="shoppingCart.item.book.inventory.quantity"/></th>
				</tr>
				<c:forEach var="item" items="${order.orderItems}">
					<tr>
						<td><a href="<spring:url value='/book/details.html'>
							<spring:param name='bookId' value='${item.orderedBook.id}' />
							</spring:url>"><c:out value="${item.orderedBook.name}"/></a></td>
						<td><c:out value="${item.orderedBook.unitPrice}"/></td>
						<td><c:out value="${item.quantity}"/></td>
						<td><c:out value="${item.totalUnitPrice}"/></td>
						<td><spring:message code="${sf:getInventoryStatusMessageCode(item.orderedBook.inventory.status)}"/></td>
						<td><c:out value="${item.orderedBook.inventory.availableQuantity}"/></td>
					</tr>
				</c:forEach>
			</table>
			<table>
				<tr>
					<th><spring:message code="confirmOrder.bookList.deliveryFee"/></th>
					<td>$<c:out value="${order.deliveryFee}"></c:out></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.bookList.totalPrice"/></th>
					<td>$<c:out value="${order.price}"></c:out></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>
			<p></p>
			<h1><spring:message code="confirmOrder.order.detailsInfo.header"/></h1>
			<table class="shoppingCart" width="100%">
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.subscriberName"/></th>
					<td><c:out value="${userAccount.userName}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.subscriberCellPhoneNumber"/></th>
					<td><c:out value="${userAccount.cellPhoneNumber}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.recipientName"/></th>
					<td><c:out value="${order.recipientName}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.recipientCellPhoneNumber"/></th>
					<td><c:out value="${order.recipientCellPhoneNumber}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.payment"/></th>
					<td><spring:message code="${sf:getPaymentMessageCode(order.payment)}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.deliveryServiceType"/></th>
					<td><c:out value="${order.deliveryService.name}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.recipientAddress"/></th>
					<td><c:out value="${order.recipientAddress}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.invoiceType"/></th>
					<td><spring:message code="${sf:getInvoiceMessageCode(order.invoice)}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.invoiceTitle"/></th>
					<td><c:out value="${order.invoice.invoiceTitle}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.invoiceUniformNumber"/></th>
					<td><c:out value="${order.invoice.uniformNumber}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.extraRequirements"/></th>
					<td><c:out value="${order.extraRequirements}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.orderStatus"/></th>
					<td><spring:message code="${sf:getOrderStatusMessageCode(order.status)}"/></td>
				</tr>
				<tr>
					<th><spring:message code="confirmOrder.order.detailsInfo.paymentStatus"/></th>
					<td><c:choose>
						<c:when test="${order.payment.paid}">
							<spring:message code="confirmOrder.order.detailsInfo.paymentStatus.yes"/>
						</c:when>
						<c:otherwise>
							<spring:message code="confirmOrder.order.detailsInfo.paymentStatus.no"/>
							<% 
							Order order = (Order)request.getAttribute("order");
							Boolean seeOrderDetailsInfo = Boolean.valueOf(request.getParameter("seeOrderDetailsInfo"));
							//if payment is not paid and is credit card, and order was created
							if((seeOrderDetailsInfo && !order.getPayment().isPaid()) && order.getPayment() instanceof CreditCard){
							%>
							<button id="payByCreditCard" class="aButton"><spring:message code="confirmOrder.creditCard.pay"/></button>
							<%
							}
							%>
						</c:otherwise>
					</c:choose>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<c:choose>
				<c:when test="${param.seeOrderDetailsInfo}">
					<td colspan="2">
						<button id="goBackToPreviousPath" class="aButton" style="float: right; margin-right: 10px;"><spring:message code="confirmOrder.goBackToPreviousPage"/></button>
					</td>
				</c:when>
				<c:otherwise>
					<td><button id="goToFillUpOrderInfo" class="aButton" style="float:left; margin-left: 10px;"><spring:message code="confirmOrder.goToFillUpOrderInfo"/></button></td>
			<td><button id="confirm" class="aButton" style="float:right; margin-right: 10px;"><spring:message code="confirmOrder.confirm"/></button></td>
				</c:otherwise>
			</c:choose>
		</tr>
	</table>
	</div>
</div>